using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

namespace App
{
	public class MdiTabStrip : ToolStrip
	{
		private MdiTabStripButton m_SelectedTab;
        private System.ComponentModel.IContainer components = null;
		public MdiTabStrip()
		{
			InitializeComponent();
            //Renderer = new ToolStripSystemRenderer();
            //this.RenderMode = ToolStripRenderMode.Professional;
            Renderer = new MdiToolRender();
            //Renderer = new Office2007Renderer();
            this.BackColor = Color.FromArgb(191, 219, 255);
            this.AutoSize = false;
            this.Size = new Size(36, 36);
            this.Dock = DockStyle.Bottom;
		}

		protected virtual MdiTabStripButton CreateMdiButton(Form f)
		{
			return new MdiTabStripButton(f);
		}

		private void FormMdiChildActivate(object sender, EventArgs e)
		{
			Form f = sender as Form;
			AddMdiChild(f.ActiveMdiChild);
			OnFormMdiChildActivate(f.ActiveMdiChild);
		}

		protected virtual void OnFormMdiChildActivate(Form activeForm)
		{
		}


		protected override void OnHandleCreated(EventArgs e)
		{
			base.OnHandleCreated(e);
			Form mdiForm = FindForm();
			if (null != mdiForm && mdiForm.IsMdiContainer)
				mdiForm.MdiChildActivate += new EventHandler(FormMdiChildActivate);
		}

		protected override void OnHandleDestroyed(EventArgs e)
		{
			base.OnHandleDestroyed(e);
			Form mdiForm = FindForm();
			if (null != mdiForm && mdiForm.IsMdiContainer)
				mdiForm.MdiChildActivate -= new EventHandler(FormMdiChildActivate);
		}

		protected override void OnItemAdded(ToolStripItemEventArgs e)
		{
			if (!(e.Item is MdiTabStripButton))
			{
				Items.Remove(e.Item);

				return;
			}
			base.OnItemAdded(e);
		}

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

        }

		[Browsable(false)]
		public new ToolStripRenderMode RenderMode
		{
			get { return ToolStripRenderMode.Custom; }
			set { }
		}

		[Browsable(false)]
		public new ToolStripGripStyle GripStyle
		{
			get { return ToolStripGripStyle.Hidden; }
			set { base.GripStyle = ToolStripGripStyle.Hidden; }
		}

		[Browsable(false)]
		public override ToolStripItemCollection Items
		{
			get { return base.Items; }
		}

		[Browsable(false)]
		public new bool ShowItemToolTips
		{
			get { return false; }
			set { base.ShowItemToolTips = false; }
		}

		[Browsable(false)]
		public MdiTabStripButton SelectedTab
		{
			get { return m_SelectedTab; }
			set
			{
				if (null == value || !Equals(value.Owner))
					return;

				if (value.Equals(m_SelectedTab))
					return;

				m_SelectedTab = value;
				foreach (MdiTabStripButton tsb in Items)
				{
					tsb.Font = Font;
					tsb.Invalidate();
				}
				m_SelectedTab.Font = new Font(Font, FontStyle.Bold);
				OnItemClicked(new ToolStripItemClickedEventArgs(value));
			}
		}

		public MdiTabStripButton FindTab(Form f)
		{
			if (null == f)
				return null;

			foreach (MdiTabStripButton tsb in Items)
			{
				if (f.Equals(tsb.GetMdiChild()))
					return tsb;
			}

			return null;
		}

		public bool ContainsMdiChild(Form f)
		{
			if (null == f)
				return false;

			foreach (MdiTabStripButton tsb in Items)
			{
				if (f.Equals(tsb.GetMdiChild()))
					return true;
			}

			return false;
		}

		public void AddMdiChild(Form f)
		{
			if (null == f)
				return;

			MdiTabStripButton tsb = FindTab(f);
			if (null != tsb)
				SelectedTab = tsb;
			else
			{
				tsb = CreateMdiButton(f);
				Items.Add(tsb);
				SelectedTab = tsb;
			}
		}

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
        }

	}

    public class MdiToolRender : Office2007Renderer
    {
        protected override void OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs e)
        {
            //base.OnRenderSplitButtonBackground(e);
            //if (e.Item.Selected || e.Item.Pressed)
            //{
            //    e.Graphics.DrawLine(new Pen(Color.FromArgb(226, 236, 248)), new Point(e.Item.Bounds.X, e.Item.Bounds.Height), new Point(e.Item.Bounds.X + e.Item.Bounds.Width, e.Item.Bounds.Height));
            //}
            //else
            //{
            //    e.Graphics.DrawLine(new Pen(Color.FromArgb(255, 255, 255)), new Point(e.Item.Bounds.X, e.Item.Bounds.Height), new Point(e.Item.Bounds.X + e.Item.Bounds.Width, e.Item.Bounds.Height));
            //}
        }

        protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
        {
            //base.OnRenderToolStripBorder(e);
            //int linePos = 4;
            //ToolStripItem item = null;
            //for (int i = 0; i < e.ToolStrip.Items.Count; i++)
            //{
            //    if (e.ToolStrip.Items[i].Selected)
            //    {
            //        item = e.ToolStrip.Items[i];
            //        break;
            //    }
            //    linePos += e.ToolStrip.Items[i].Size.Width + 8;
            //}


            //if (item == null) return;
            //e.Graphics.DrawLine(new Pen(Color.FromArgb(141, 178, 227)), new Point(0, e.ClipRectangle.Height), new Point(e.ClipRectangle.Width, e.ClipRectangle.Height));

            //e.Graphics.DrawLine(new Pen(Color.FromArgb(141, 178, 227)), new Point(1, (int)e.Graphics.ClipBounds.Height - 0), new Point(linePos, (int)e.Graphics.ClipBounds.Height - 0));

            //e.Graphics.DrawLine(new Pen(Color.FromArgb(141, 178, 227)), new Point(linePos + item.Size.Width + 4, (int)e.Graphics.ClipBounds.Height - 0), new Point((int)e.Graphics.ClipBounds.Width, (int)e.Graphics.ClipBounds.Height - 0));
        }
    }
}
